package com.yssoft.service.production;


import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import com.yssoft.entity.production.TNonConformity;
import com.yssoft.entity.production.TNonConformityImg;
import com.yssoft.mapper.production.TNonConformityImgMapper;
import com.yssoft.mapper.production.TNonConformityMapper;
import com.yssoft.utils.AppUserHelper;
import com.yssoft.utils.AppUtil;
import com.yssoft.utils.UploadFile;

/**
 * 不合格品管理Service
 * @author 王永宁
 * @time 2018年9月7日15:49:55
 */
@Service
public class NonConformityService {
	
	@Resource
	private TNonConformityMapper nonConformityMapper;
	
	@Resource
	private TNonConformityImgMapper nonConformityImgMapper;

	/**
	 * 保存入不合格品库
	 * @param id
	 * @param warehouseId
	 * @param teId
	 * @param request
	 */
	public void save(Integer id, Integer warehouseId, Integer teId, HttpServletRequest request) {
		TNonConformity nonConformity = new TNonConformity();
		nonConformity.settEId(teId);
		nonConformity.settInstockAppId(id);
		nonConformity.settWarehouseId(warehouseId);
		nonConformity.setCode("NC"+new Date().getTime());
		nonConformity.setStat("0");
		nonConformity.setType("1");
		nonConformity.setDeleteFlag("0");
		nonConformity.setCreateTime(new Date());
		nonConformity.setCreatorId(AppUserHelper.getCurrentUserId(request));
		nonConformity.setCreatorName(AppUserHelper.getCurrentUserName(request));
		nonConformity.setDeptId(AppUserHelper.getCurrentDeptId(request));
		nonConformity.setDeptName(AppUserHelper.getCurrentDeptName(request));
		nonConformityMapper.insert(nonConformity);
		
	}

    /**
     * @方法名称: getNonConformityList
     * @功能描述: 获取不合格品列表
     * @作者: 王永宁
     * @创建时间:2018年9月8日09:49:43
     * @param page
     * @param limit
     * @param companyId
     * @param stat
     * @return List<Map<String,Object>>
     */
	public List<Map<String, Object>> getNonConformityList(int page, int limit,
			Integer companyId, String stat) {
		HashMap<String,Object> param = new HashMap<>();
        param.put("stat",stat);
        param.put("companyId",companyId);
        int start = ((page - 1) * limit);
		int end = limit;
		param.put("start", start);
		param.put("end", end);
		List<Map<String, Object>>  nonConformityList = nonConformityMapper.getNonConformityList(param);
        return nonConformityList;
    }

	/**
     * @方法名称: handle
     * @功能描述: 不合格品处理
     * @作者: 王永宁
     * @创建时间:2018年9月8日14:51:13
	 */
	@Transactional(rollbackFor = Exception.class)
	public Object handle(Integer id, Date processingDate,String processingPerson, String processingMethod,BigDecimal processingQuantity, HttpServletRequest request,MultipartFile[] files) {
		TNonConformity nonConformity = nonConformityMapper.selectByPrimaryKey(id);
		if(nonConformity !=null){
		nonConformity.setProcessingDate(processingDate);
		nonConformity.setProcessingPerson(processingPerson);
		nonConformity.setProcessingQuantity(processingQuantity);
		nonConformity.setProcessingMethod(processingMethod);
		nonConformity.setStat("1");
		nonConformityMapper.updateByPrimaryKeySelective(nonConformity);
			//保存附件
			MultipartRequest mRequest=(MultipartRequest)request;
			Iterator<String> fileNames = mRequest.getFileNames();
			while(fileNames.hasNext()){
				MultipartFile file = mRequest.getFile(fileNames.next());
				Map<String,Object> urlMap = null;
                try {
                    urlMap = UploadFile.compressUploadedFile(file);
                } catch (Exception e) {
                    return AppUtil.returnMapJson("0", "上传图片失败" + e.getMessage(), null);
                }
               
                TNonConformityImg att = new TNonConformityImg();
                att.setUrl(urlMap.get("fileName")+"");
                att.setTurl(urlMap.get("thumbnails")+"");
                att.settNonConformityId(id);
                att.setCreateTime(new Date());
                nonConformityImgMapper.insertSelective(att);
			}
			return AppUtil.returnMapJson("1", "保存成功!", null);
		} else {
			return AppUtil.returnMapJson("0", "保存失败!", null);
		}
		
	}

	/**
	 * 获取不合格品处理记录
	 * @param id
	 * @return
	 */
	public Map<String, Object> getNonConformityDetail(Integer id) {
		Map<String, Object> nonConformity = nonConformityMapper.getOne(id);
		nonConformity.put("pic", nonConformityImgMapper.selectByInstockAppId(id));
		return nonConformity;
	}

	


}
